﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlphanumericPhone
{
    /// <summary>
    /// http://www.careercup.com/question?id=4655753138798592
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// Print the actual phone number when given an alphanumeric phone number. 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        /// <example>e.g. an input of 1-800-COM-CAST should give output as 18002662278</example>
        public static string GetPhoneNumber(this string number)
        {
            string map = "22233344455566677778889999";

            string result = string.Empty;

            foreach (var n in number)
            {
                if (n >= 48 && n <= 57)             //[0-9]
                    result += n;
                else if (n >= 65 && n <= 90)        //[A-Z]
                    result += map[n - 65];
                else if(n >= 97 && n <= 122)        //[a-z]
                    result += map[n - 97];
            }

            return result;
        }
        
    }
}
